package com.laboratory.eventlistenerlearning.listener;

import com.laboratory.eventlistenerlearning.event.RoleEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 角色事件监听器
 */
@Component
public class RoleListener {

    /**
     * 提交之前触发
     *
     * @param event
     */
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
    public void beforeCommit(RoleEvent event) {
        System.out.println("RoleListener.beforeCommit()");
        System.out.println(event.message());
    }

    /**
     * 提交之后触发
     *
     * @param event
     */
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void afterCommit(RoleEvent event) {
        System.out.println("RoleListener.afterCommit()");
        System.out.println(event.message());
    }

    /**
     * 提交触发
     *
     * @param event
     */
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
    public void afterCompletion(RoleEvent event) {
        System.out.println("RoleListener.afterCompletion()");
        System.out.println(event.message());
    }

    /**
     * 回滚触发
     *
     * @param event
     */
    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
    public void afterRollback(RoleEvent event) {
        System.out.println("RoleListener.afterRollback()");
        System.out.println(event.message());
    }

}
